# -*- coding: utf-8 -*-
import time
import os, sys, pdb
import hashlib
import traceback


def isMatch(nowStr, matchs):
    '''
        判断nowStr是否部分匹配matchs中任意一个
    '''
    for nowEx in matchs:
        if (nowStr.find(nowEx) != -1):
            return True
    return False


def bitMD5(fileBit):
    md5obj = hashlib.md5()
    md5obj.update(fileBit)
    hash = md5obj.hexdigest()
    # print(hash)
    return hash


def fileMD5(filepath):
    with open(filepath, 'rb') as f:
        return bitMD5(f.read())


# pdb.set_trace()
# imgPool = Pool(10)
# 写入剪切板
def setClipboard(data):
    os.system(r'ECHO %s|CLIP' % data)


def printMy(*objects, sep=' ', end='\n', file=sys.stdout, flush=False):
    """
    打印并记录日志
    :param objects:
    :param sep:
    :param end:
    :param file:
    :param flush:
    :return:
    """
    nowDateTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    objects = [nowDateTime, *objects]
    print(*objects, sep=sep, end=end, file=file, flush=flush)
    try:
        print(*objects, sep=sep, end=end, file=open("./log.log", 'a'), flush=flush)
    except BaseException as e:
        print(traceback.format_exc())
        pass


def wFile(file, text, append='a'):
    """
    写入文件
    :param file:
    :param text:
    :param append:
    :return:
    """
    with open(file, append + "b") as f:
        f.write(text.encode("utf8", errors="ignore"))


def wFileB(file, bText):
    with open(file, 'wb') as f:
        f.write(bText)


def filterFileFun(fileName, *suffix):
    """
    过滤文件类型

    :param fileName:
    :param suffix:
    :return:
    """
    try:
        for nowSuffix in suffix:
            if fileName.endswith("." + nowSuffix) or fileName.endswith("." + nowSuffix.swapcase()):
                return True
    except BaseException as e:
        printMy(traceback.format_exc(e))
    return False
